package com.sinoglobal.ningxia.activity.interestingGame;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.adapter.InteractionAnswerErrorGdAdapter;
import com.sinoglobal.ningxia.beans.InteractionAnswerListItemsVo;
import com.sinoglobal.ningxia.frame.AbstractActivity;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.TextUtil;

/**
 * @author sbk
 * @createdate 2013-12-12 下午2:18:11
 * @Description: 查看错题
 */
public class AnswerLookErrorActivity extends AbstractActivity implements OnClickListener {

	private GridView aGridView;
	private TextView tvTitle;
	private List<String> aListItem;
	private List<InteractionAnswerListItemsVo> aList, tempList;
	private int index = 0; // 第几题
	private InteractionAnswerErrorGdAdapter aAdapter;

	// private ProgressBar sPro, tPro, aPro;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.interaction_answer_layout);
		findViewById(R.id.rela1).setVisibility(View.GONE);
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;

		int height = (int) (1.136 * screenWidth);

		LinearLayout bgLayout = (LinearLayout) findViewById(R.id.l10);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth, height);
		params.topMargin = EquipmentUtil.px2dip(this, 40);
		bgLayout.setLayoutParams(params);
		aGridView = (GridView) findViewById(R.id.gv_id);
		tvTitle = (TextView) findViewById(R.id.text10);
		titleView.setText(getString(R.string.check_error));
		templateButtonRight.setBackgroundResource(R.drawable.icon_next);
		templateButtonRight.setVisibility(View.VISIBLE);
		templateButtonRight.setOnClickListener(this);
		templateButtonLeft.setOnClickListener(this);
		tempList = (ArrayList<InteractionAnswerListItemsVo>) getIntent().getSerializableExtra("mList");
		aList = new ArrayList<InteractionAnswerListItemsVo>();
		aAdapter = new InteractionAnswerErrorGdAdapter(this);
		aGridView.setAdapter(aAdapter);
		if (tempList.size() == 1) {
			templateButtonRight.setVisibility(View.INVISIBLE);
		}
		for (int i = 0; i < tempList.size(); i++) {
			if (tempList.get(i).isError()) {
				aList.add(tempList.get(i));
			}
		}

		loadSubject(index);
	}

	/**
	 * @author sbk
	 * @createdate 2013-12-15 下午1:37:04
	 * @Description: 加载kaoti
	 * @param i
	 * 
	 */
	private void loadSubject(int i) {
		aListItem = new ArrayList<String>();
		InteractionAnswerListItemsVo t = aList.get(i);
		tvTitle.setText(t.getAnswer_name());
		aListItem.add(t.getAaa());
		aListItem.add(t.getBbb());
		if (TextUtil.stringIsNotNull(t.getCcc())) {
			aListItem.add(t.getCcc());
		}
		if (TextUtil.stringIsNotNull(t.getDdd())) {
			aListItem.add(t.getDdd());
		}
		if ("1".equals(t.getType())) {
			aGridView.setNumColumns(2);
			aGridView.setVerticalSpacing(5);
		} else {
			aGridView.setNumColumns(1);
			aGridView.setVerticalSpacing(25);
		}

		aAdapter.setaListItem(aListItem, aList.get(i).getSelect(), getNum(aList.get(i).getAnswer_right()));
		aAdapter.notifyDataSetChanged();
		setTitleRight(index, aList);
	}

	/**
	 * @author sbk
	 * @createdate 2013-12-26 下午3:51:24
	 * @Description: 返回正确答案
	 * @param arg
	 * @return
	 * 
	 */
	private int getNum(String arg) {
		int res = -1;
		if ("a".equalsIgnoreCase(arg)) {
			res = 0;
		} else if ("b".equalsIgnoreCase(arg)) {
			res = 1;
		} else if ("c".equalsIgnoreCase(arg)) {
			res = 2;
		} else if ("d".equalsIgnoreCase(arg)) {
			res = 3;
		}
		return res;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_but_left: // 返回
			if (index > 0) {
				if (index != -1) {
					index--;
				}
				loadSubject(index);
			} else {
				finish();
			}
			break;

		case R.id.title_but_right:// next
			if (index < aList.size()) {
				if (index != aList.size() - 1) {
					index++;
				} else {
					// showShortToastMessage("已经是最后一题了");
					return;
				}

				loadSubject(index);
			}
			break;
		}
	}

	private void setTitleRight(int index, List<InteractionAnswerListItemsVo> list) {
		if (index == list.size()-1) {
			templateButtonRight.setVisibility(View.INVISIBLE);
		} else {
			templateButtonRight.setVisibility(View.VISIBLE);
		}
	}
}
